package com.exam.controller;

import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.exam.pojo.Brand;
import com.exam.service.BrandService;
import entity.Result;


import java.util.List;

@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    public BrandService brandService;

    @GetMapping
    public Result<Brand> findAll(){
        List<Brand> brandList=brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList);
    }
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brand);
    }
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable Integer id){
        //设置ID
        brand.setId(id);
        //修改数据
        brandService.updateByPrimaryKey(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    @PostMapping(value = "/search" )
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",list);
    }
}
